package com.smasher.print.em;

import android.os.Handler;

import com.printsdk.PrintSerializable;
import com.smasher.print.core.PrintField;
import com.smasher.print.core.PrintTemplate;
import com.smasher.print.em.template.EMPrintField;
import com.smasher.print.em.template.EMPrintTemplate;
import com.smasher.print.em.template.FontConfig;
import com.smasher.print.em.template.TemplatePrintFactory;

import org.json.JSONObject;

public class PrintContent {

    public static final String TAG = "PrintContent";

    public static boolean mBBeeper = false;

    private final PrintSerializable mPrinter;
    private final Handler mHandler;

    public PrintContent(PrintSerializable printer, Handler handler) {
        mPrinter = printer;
        mHandler = handler;
    }


    //打印文字
    protected void printText(JSONObject json) {
        if (mPrinter.getState() != PrintSerializable.CONN_SUCCESS) {
            return;
        }

        new Thread() {
            @Override
            public void run() {

                PrintTemplate templateTemp = TemplatePrintFactory.createBryDbTemplate(json);

                EMPrintTemplate template = (EMPrintTemplate) templateTemp;

                // 初始化
                mPrinter.init();
                mPrinter.wrapLines(2);

                // 打印标题
                if (template.getTitle() != null) {
                    FontConfig titleFont = template.getTitleFont();
                    mPrinter.setFont(titleFont.getWidth(), titleFont.getHeight(),
                            titleFont.getBold(), titleFont.getUnderline());
                    mPrinter.setAlign(template.getTitleAlign());
                    mPrinter.printText(template.getTitle());
                    mPrinter.wrapLines(template.getTitleGap());
                }

                // 打印各个字段
                for (PrintField fieldTemp : template.getFields()) {
                    if (fieldTemp instanceof EMPrintField) {
                        EMPrintField field = (EMPrintField) fieldTemp;
                        FontConfig fieldFont = field.getFont();
                        mPrinter.setFont(fieldFont.getWidth(), fieldFont.getHeight(),
                                fieldFont.getBold(), fieldFont.getUnderline());
                        mPrinter.setAlign(field.getAlign());
                        mPrinter.printText(field.getLabel() + field.getValue() + "\n");
                        mPrinter.wrapLines(field.getGap());
                    }
                }

                // 打印页脚
                if (template.getFooter() != null) {
                    FontConfig footerFont = template.getFooterFont();
                    mPrinter.setFont(footerFont.getWidth(), footerFont.getHeight(),
                            footerFont.getBold(), footerFont.getUnderline());
                    mPrinter.setAlign(template.getFooterAlign());
                    mPrinter.printText(template.getFooter());
                    mPrinter.wrapLines(template.getFooterGap());
                }

                mPrinter.setFont(0, 0, 0, 0);          // 设置字体大小
                mPrinter.wrapLines(2);

                if (mBBeeper) {
                    mPrinter.Beeper((byte) 10);
                }
                mHandler.obtainMessage(50000).sendToTarget();
            }
        }.start();
    }

}
